package com.william.jdbcplus.core.dialect.ddl.index.impl;

import com.william.jdbcplus.common.entity.Entity;
import com.william.jdbcplus.core.dialect.ddl.index.IIndex;
import com.william.jdbcplus.core.meta.TableHelperUtil;
import com.william.jdbcplus.core.meta.TableIndexInfo;
import com.william.jdbcplus.core.meta.TableInfo;
import com.william.jdbcplus.core.toolkit.StringPool;

import java.util.stream.Collectors;

/**
 * Mysql 索引实现
 *
 * @author william
 */
public class MysqlIndex implements IIndex {
    /**
     * 新建索引
     *
     * @param clazz
     * @param indexInfo
     * @return
     */
    @Override
    public <M extends Entity> String add(Class<M> clazz, TableIndexInfo indexInfo) {
        TableInfo tableInfo = TableHelperUtil.getTableInfo(clazz);
        StringBuffer sqlBuffer = new StringBuffer();
        String columnNames = indexInfo.getColumns().stream().collect(Collectors.joining(StringPool.COMMA));
        if (!indexInfo.isUnique()) {
            sqlBuffer.append("create index " + indexInfo.getIndexName() + " on " + tableInfo.getTableName() + " (" + columnNames + ");");
        } else {
            sqlBuffer.append("create unique index " + indexInfo.getIndexName() + " on " + tableInfo.getTableName() + " (" + columnNames + ");");
        }

        return sqlBuffer.toString();
    }

    /**
     * 修改索引
     *
     * @param clazz
     * @param indexInfo
     * @return
     */
    @Override
    public <M extends Entity> String modify(Class<M> clazz, TableIndexInfo indexInfo, TableIndexInfo dbIndex) {
        TableInfo tableInfo = TableHelperUtil.getTableInfo(clazz);
        StringBuffer sqlBuffer = new StringBuffer();
        String columnNames = indexInfo.getColumns().stream().collect(Collectors.joining(StringPool.COMMA));
        String indexName = dbIndex.getIndexName();
        if (indexName.toLowerCase().startsWith("uk_")) {
            sqlBuffer.append(dropUk(clazz, dbIndex));
        } else {
            sqlBuffer.append(drop(clazz, dbIndex));
        }
        if (!indexInfo.isUnique()) {
            sqlBuffer.append(StringPool.NEWLINE).append("create index " + indexInfo.getIndexName() + " on " + tableInfo.getTableName() + " (" + columnNames + ");");
        } else {
            sqlBuffer.append(StringPool.NEWLINE).append("create unique index " + indexInfo.getIndexName() + " on " + tableInfo.getTableName() + " (" + columnNames + ");");
        }

        return sqlBuffer.toString();
    }

    /**
     * 删除索引
     *
     * @param clazz
     * @param dbIndex
     * @return
     */
    @Override
    public <M extends Entity> String drop(Class<M> clazz, TableIndexInfo dbIndex) {
        TableInfo tableInfo = TableHelperUtil.getTableInfo(clazz);
        StringBuffer sqlBuffer = new StringBuffer();
        String indexName = dbIndex.getIndexName();
        sqlBuffer.append("drop index " + indexName + " on " + tableInfo.getTableName() + ";");
        return sqlBuffer.toString();
    }

    @Override
    public <M extends Entity> String dropUk(Class<M> clazz, TableIndexInfo dbIndex) {
        TableInfo tableInfo = TableHelperUtil.getTableInfo(clazz);
        String ukIndexName = dbIndex.getIndexName();
        StringBuffer sqlBuffer = new StringBuffer();
        sqlBuffer.append("alter table " + tableInfo.getTableName() + " drop constraint " + ukIndexName + ";");
        return sqlBuffer.toString();
    }
}
